package com.kafka.service;

import java.io.IOException;
import java.util.Properties;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kafka.core.BaseClass;
import com.kafka.enumtype.MsgType;
import com.kafka.producer.AccountProducer;
import com.kafka.producer.DeviceProducer;
import com.kafka.producer.PayProducer;
import com.kafka.producer.MedicalProducer;
import com.kafka.producer.UserProducer;
import com.kafka.util.AES;
import com.kafka.util.BaseUtil;

/**
 * 会员消费消息
 * 
 * @author 雷洪岸
 * 
 */
@Path("msg")
public class MsgResource extends BaseClass{
	private static PayProducer cp = new PayProducer();
	private static UserProducer up = new UserProducer();
	private static AccountProducer ap = new AccountProducer();
	private static DeviceProducer dp = new DeviceProducer();
	private static MedicalProducer pep = new MedicalProducer();
	private static Logger log = LoggerFactory.getLogger(MsgResource.class);
	// 解密密钥
	private static String skey;

	public MsgResource() {
		Properties properties = new Properties();
		try {
			properties.load(ClassLoader
					.getSystemResourceAsStream("sysconfig.properties"));
			skey = properties.getProperty("sKey");
		} catch (IOException e) {
			e.printStackTrace();
		}
		PropertyConfigurator.configure("config/log4j.properties");
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public String getConsumeMessage(@Valid String message) {
		JSONObject jsonMsg = null;
		try {
			// 数据解密
			if (!message.isEmpty()) {
				try {
					message = AES.Decrypt(message, skey);
					log.info("###Decrypt:" + message.toString() + ",time:" + BaseUtil.getDate());
				} catch (Exception e) {
					log.info("###Decrypt:" + "解密数据时发生异常:" + e.toString() + ",time:" + BaseUtil.getDate());
					return failMsg(JSONObject.fromObject(message), "解密数据时发生异常:"+e.toString());
				}
			}
			jsonMsg = rebuildJSON(JSONObject.fromObject(message));
			if (jsonMsg != null) {
				//重构json数据
				if(MsgType.pay.toString().equals(jsonMsg.getString("msgtype"))){
					jsonMsg.put("datas", rebuildJSONArray(jsonMsg.getJSONArray("datas")));
				}else{
					jsonMsg.put("datas", rebuildJSON(jsonMsg.getJSONObject("datas")));
				}
				if (MsgType.pay.toString().equals(jsonMsg.getString("msgtype"))
						|| MsgType.RECHARGE.toString().equals(jsonMsg.getString("msgtype"))) {
					// 产品消费
					log.info("###message:" + message.toString() + ",time:" + BaseUtil.getDate());
					return cp.produce(jsonMsg);
				} else if (MsgType.REGISTER.toString().equals(jsonMsg.getString("msgtype"))
						||MsgType.UDATE.toString().equals(jsonMsg.getString("msgtype"))
						|| MsgType.ACTIVATEUSER.toString().equals(jsonMsg.getString("msgtype")) 
						|| MsgType.USERBLACKLIST.toString().equals(jsonMsg.getString("msgtype")) 
						|| MsgType.CLOSEUSER.toString().equals(jsonMsg.getString("msgtype"))) {
					// 用户信息修改
					log.info("###message:" + message.toString() + ",time:" + BaseUtil.getDate());
					return up.produce(jsonMsg);
				} else if (MsgType.BINDACCOUNT.toString().equals(jsonMsg.getString("msgtype")) 
						|| MsgType.UNBINDACCOUNT.toString().equals(jsonMsg.getString("msgtype")) 
						|| MsgType.LOSTCARD.toString().equals(jsonMsg.getString("msgtype")) 
						|| MsgType.ACTIVATECARD.toString().equals(jsonMsg.getString("msgtype"))
						|| MsgType.CARDBLACKLIST.toString().equals(jsonMsg.getString("msgtype"))
						|| MsgType.UPDATEPAYPASSWD.toString().equals(jsonMsg.getString("msgtype"))) {
					// 绑定卡或解绑卡或注销
					log.info("###message:" + message.toString() + ",time:" + BaseUtil.getDate());
					return ap.produce(jsonMsg);
				} else if (MsgType.BINDDEVICE.toString().equals(
						jsonMsg.getString("msgtype")) || MsgType.UNBINDDEVICE.toString().equals(
								jsonMsg.getString("msgtype"))) {
					// 绑定设备或解绑设备
					log.info("###message:" + message.toString() + ",time:" + BaseUtil.getDate());
					return dp.produce(jsonMsg);
				} else if(MsgType.MEDICAL.toString().equals(jsonMsg.getString("msgtype"))){
					// 体检数据
					return pep.produce(jsonMsg);
				} else if (MsgType.DELETE.toString().equals(
						jsonMsg.getString("msgtype"))) {
					return successMsg(jsonMsg, "用户删除功能还没有实现！");
				} else {
					return failMsg(jsonMsg, "没有匹配的消息类型！");
				}
			}else{
				return failMsg(jsonMsg, "json数据格式不正确或串为空！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(jsonMsg, "json格式错误:"+e.toString());
		}
	}
}
